﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
///DocumentUploadEditShow 的摘要说明
/// </summary>
public class DocumentUploadEditShow:BaseEditShowField
{
	public DocumentUploadEditShow()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    public override IEnumerable<Control> GetThisTypeControl(ConfigColumns cCol, string value, bool isEdit)
    {
        HyperLink link = new HyperLink();
        link.ID = cCol.ColumnName + "_link";
        link.Text = "查看文件";
        link.Target = "_blank";
        if (isEdit)
        {
            link.NavigateUrl = "../uploads/" + value;
        }

        HiddenField hide = new HiddenField();
        hide.ID = cCol.ColumnName + "_EditShow";
        AjaxFileUpload uploader = new AjaxFileUpload();
        uploader.AllowExt = cCol.EditShowAllowUploadExt;
        uploader.ID = cCol.ColumnName + "_uploader";
        uploader.StaticScript = false;

        uploader.OnSuccessScript = @"function (data, status)
                            {
                                if (typeof (data.error) != 'undefined')
                                {
                                    if (data.error != '')
                                    {
                                        alert(data.error);
                                    } 
                                    else
                                    {
                                        {dododo}
                                    }
                                }
                            }";
        string successDo = "$(\"#" + link.ID + "\").attr(\"href\",'../uploads/'+data.msg).show();$(\"#" + hide.ID + "\").val(data.msg);";
        uploader.OnSuccessScript = uploader.OnSuccessScript.Replace("{dododo}", successDo);

        string script = "$(function () {";
        if (!isEdit)
        {
            script += "$(\"#" + link.ID + "\").hide()";
        }
        else
        {
            if (string.IsNullOrEmpty(value))
            {
                script += "$(\"#" + link.ID + "\").hide()";
            }
        }
        script += "})";

        Helper.WriteScriptAtTop(script, cCol.ColumnName + "_SetupScript");

        yield return link;
        yield return hide;
        yield return uploader;
    }
}
